package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.Task;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.TaskService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: uqbike-manager-new
 * @description: 语音升级任务控制层
 * @author: Jing
 * @create: 2021-05-24 15:11
 **/
@Slf4j
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;

    /**
     * 查询登入账号的所有升级任务
     *
     * @param task
     * @return
     */
    @GetMapping("/list")
    public JsonResult list(Task task,int pageNum,int pageSize) {
        Map<String, Object> map = taskService.selectTaskUser(task,pageNum,pageSize);
        if (map.isEmpty()) {
            return JsonResult.ok(null, 0);
        }
        return JsonResult.ok(map.get("data"), Objects.nonNull(map.get("total")) ? Integer.parseInt(map.get("total").toString()) : 0);
    }

    /**
     * 查询升级任务执行结果
     *
     * @param taskId
     * @return
     */
    @GetMapping("/findByTaskId")
    public JsonResult getInfo(@RequestParam Integer taskId, @RequestParam Integer accountId) {
        Map<String, Object> map = taskService.getInfo(taskId, accountId);
        List<Task> taskList = Optional.ofNullable(map.get("data"))
                .map(Object::toString)
                .map(x -> JSON.parseArray(x, Task.class))
                .orElse(Collections.emptyList());
        Map<Object, Long> taskMap = taskList.stream().collect(Collectors.groupingBy(Task::getExeRet, Collectors.counting()));
        return JsonResult.ok(map.get("data"), Integer.parseInt(map.get("total").toString()), taskMap);
    }


    /**
     * 添加升级任务
     *
     * @param task
     * @returnx
     */
    @PostMapping(value = "/add")
    @OperateLog(module = ModuleEnum.OTA, methods = MethodEnum.OTA_ADD)
    public Result<Object> add(@Validated Task task) {
        return taskService.add(task);
    }

    /**
     * 修改任务升级状态
     *
     * @param task
     * @return
     */
    @PostMapping(value = "/edit")
    @OperateLog(module = ModuleEnum.OTA, methods = MethodEnum.OTA_EDIT)
    public JsonResult edit(Task task) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        return JsonResult.toAjax(taskService.edit(task), context);
    }


    /**
     * 查询单个设备的执行结果
     *
     * @param task
     * @return
     */
    @PostMapping(value = "/selectOne")
    public JsonResult selectOne(Task task) {
        Map<String, Object> map = taskService.selectTerMno(task);
        return JsonResult.ok(map.get("data"));
    }


}
